C++ Basics

Exceptions in C++

What is exception?

An exception is an anomaly that occurs at run-time in software and disrupts the normal flow of the program. To best describe exceptions, it is better to look at a an examples. Lets take for example the process of writing to a file. If the file that we are trying to change is corrupted, then the operating system will throw an exception.

Exceptions disrupt the normal flow of your program. Yet, your application can handle exceptions and display friendly error messages to the user, perform any minimal rescue operation if needed, and exit gracefully, whenever an exception occurs.

What causes an exception?

Exceptions can be caused by external factors, such as a system with insufficient resources, or by factors internal to your application, such as a pointer that is used in despite it containing an invalid value or a divide-by-zero error.

Dealing with anomalous behavior can be one of the most difficult parts of designing any system.

To protect you code against exceptions, you will need to "handle" exceptions when they occur thereby making you code "exception safe".